package testdb4o;

import java.util.Set;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.wgo.surveyModel.domain.common.Hydrophone;
import com.wgo.surveyModel.domain.common.Streamer;
import com.wgo.surveyModel.domain.common.StreamerSection;
import com.wgo.surveyModel.domain.common.Vessel;
import com.wgo.surveyModel.domain.common.impl.DomainFactory;
import com.wgo.surveyModel.domain.common.impl.VesselImpl;




public class TestDb4Object
{
    static ObjectContainer db = null;
    static final String dbFileName = "survey.db";

    public void run()
    {
        DomainFactory df = new DomainFactory();
        Vessel vessel = null;
 
        int noObj = 0;
        
 
        for( int v = 0; v < 10; v++)
        {
            noObj++;
            vessel= df.createVessel();
            vessel.setName("vessel-" + v);
            vessel.setProdYear(1995);
            vessel.setWeight(new Float(55.5));
           
            Streamer streamer = null;
            for( int s = 0; s < 20; s++)
            {
                noObj++;
                streamer= df.createStreamer();
                streamer.setSerialNumber("streamer-" + s);
                streamer.setProdYear(1995);
                streamer.setLenght(9000);
                streamer.setType("solid");
                
                vessel.addStreamer(streamer);
                
                StreamerSection section = null;
                for( int ss = 0; ss < 20; ss++)
                {
                    noObj++;
                    section= df.createStreamerSection();
                    section.setSerialNumber("section-" + ss);
                    section.setProdYear(1995);
                    section.setLenght(9000);
                    section.setType("live");
                    
                    streamer.addSection(section);
                    
                    Hydrophone hyd = null;
                    for( int h = 0; h < 20; h++)
                    {
                        noObj++;
                        hyd= df.createHydrophone();
                        hyd.setSerialNumber("hydrophone-" + h);
                        hyd.setProdYear(1995);
                        hyd.setFilter(10); 
                        
                        section.addHydrophone(hyd);
                                                 
                    }                                            
                }                                                    
            }
            
            db.set(vessel);
            db.commit();
        }
                            
        db.commit();
        System.out.println("Saved objects : " + noObj);
    }
    
    public void readData()
    {
        //List<Car> cars = db.guery (CarImpl.class)
        
        int nofObj = 0;
        int filt = 0;
        ObjectSet result = db.get(VesselImpl.class);
        while(result.hasNext())
        {
            nofObj++;
            Vessel vessel = (Vessel)result.next();
            System.out.println("Vesselname : " + vessel.getName());
            
            Set<Streamer> streamers = vessel.getStreamers();
            for(Streamer s : streamers)
            {
                nofObj++;
                System.out.println("  Streamer # = " + s.getSerialNumber());
                
                Set<StreamerSection> ssections = s.getSections();
                for(StreamerSection  ss : ssections)
                {
                    nofObj++;
                    System.out.println("     Section # = " + ss.getSerialNumber());
                    
                    Set<Hydrophone> hydrophones = ss.getHydrophones();
                    System.out.println("       hyd no = " + hydrophones.size());
                    for(Hydrophone  h : hydrophones)
                    {
                        nofObj++;
                        filt += h.getFilter();
                        //System.out.println("       hyd # = " + h.getSerialNumber());                                               
                    }
                }
            }
            
        }
        
        System.out.println("no objects read = " + nofObj + " filt = " + filt);
    }
  
    public static void main(String[] args) 
    {
        //new File(dbFileName).delete();
        
        Db4o.configure().activationDepth(6);
        //Db4o.configure().objectClass(
        //   "com.wgo.carModel.common.domain.impl.StreamerSectionImpl").cascadeOnUpdate(true);
        
        db = Db4o.openFile(dbFileName);
               
        try
        {
            TestDb4Object me = new TestDb4Object();
            
            
            long t1 = System.currentTimeMillis();
            me.run();
            long t2 = System.currentTimeMillis();
            System.out.println("Save time = " + (t2-t1)/1000.0);
            
            Thread.sleep(5000,0);
            
            t1 = System.currentTimeMillis();
            me.readData(); 
            t2 = System.currentTimeMillis();
            System.out.println("read time = " + (t2-t1)/1000.0);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("Closing database!");
            db.close();
        }         
    }
}
